<?php
namespace app\index\controller;

use app\base\Auth;
use app\base\BaseController;
use app\base\model\Books as booksModel;
use app\base\model\Borrow as BorrowModel;
use think\Db;
use think\Request;
use think\Session;

class Index extends BaseController
{
    /***
     * 输出首页视图
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $request = Request::instance();
        $books = new booksModel();
        $page = $request->has('page') ? $request->param('page') : 1;
        $limit = $request->has('limit') ? $request->param('limit') : 5;
        $offset = $limit * $page-$limit;

        $books_list = $books->limit($offset, $limit)->select();

        if($books_list) {
            $books_list = collection($books_list)->toArray();
        }

        $this->assign('books_count',$books->count('book_id'));
        $this->assign('limit',$limit);
        $this->assign('page',$page);
        $this->assign('books_list',$books_list);
        return $this->fetch();
    }

    /***
     * 借阅书籍
     * @return array
     */
    public function addBorrow()
    {

        if (!Auth::isLogin()) {
            return ['code'=>-1,'msg'=>'请登录'];
        }
        $request = Request::instance();
        $book_id = $request->param('book_id');
        $timep = time();

        $book_=booksModel::get($book_id);
        if ($book_->book_num<1) {
            return ['code'=>-1,'msg'=>'库存不足'];

        }


        Db::startTrans();
        try{
            Db::name('borrow')->insert([
                'book_id' => $book_id,
                'user_id' => Session::get('user')['user_id'],
                'borrow_time' => $timep,
                'borrow_return_time' => $timep + (30 * 24 * 60 * 60)
            ]);
            Db::name('books')->where('book_id', $book_id)->setDec('book_num');
            // 提交事务
            Db::commit();

            return ['code'=>1,'msg'=>'借阅成功'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }






    }

    /***
     * 借阅查询
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function borrowAjax()
    {

        if (!Auth::isLogin()) {
            return ['code'=>-1,'msg'=>'请求方式错误'];
        }

        $request = Request::instance();
        if (!$request->isAjax()) {
            return ['code'=>-1,'msg'=>'请求方式错误'];
        }

        $uid = Session::get('user')['user_id'];

        $borrow = new BorrowModel();
        $limit = $request->param('limit');
        $offset = $limit * $request->param('page')-$limit;
        $total = $borrow->where('user_id',$uid)->count('book_id');
        $data = $borrow->where('user_id',$uid)->limit($offset,$limit)->select();
        return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $data];
    }
}
